local yijue = fk.CreateSkill {
  name = "klmou__yijue",
  tags = { Skill.Compulsory }
}

Fk:loadTranslationTable {
  ["klmou__yijue"] = "义绝",
  [":klmou__yijue"] = "每轮限一次，当你的回合结束阶段开始时，若你的手牌区只有一种颜色的牌，你可以选择一项："..
  "<br>1.将自己的手牌补至体力上限并可以令一名受伤的其他角色回复一点体力；<br>2.令自己执行一个额外的出牌阶段并可以让一名受伤的其他角色回复一点体力。",

  ["#klmou__yijue-draw"] = "将手牌补至体力上限",
  ["#klmou__yijue-play"] = "执行一个额外的出牌阶段",

  ["$klmou__yijue1"] = "今释君权报厚遇，此一别复无旧恩！",
  ["$klmou__yijue2"] = "放公归去已全义，再擒尽忠不容情！",
}

yijue:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(yijue.name) and player.phase == Player.Finish and player:usedEffectTimes(self.name, Player.HistoryRound) < 1 then
      local cards = player:getCardIds("h")
      return #cards > 0 and table.every(cards, function(id) return Fk:getCardById(id).color == Fk:getCardById(cards[1]).color end)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local ch = room:askToChoice(player, {
      choices = { "Cancel", "#klmou__yijue-draw", "#klmou__yijue-play" },
      skill_name = yijue.name,
      prompt = "义绝：你可以执行一项并可以令一名受伤的其他角色回复1点体力",
    })
    if ch ~= "Cancel" then
      event:setCostData(self, { choice = ch })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    local play = false
    if choice == "#klmou__yijue-draw" then
      local num = player.maxHp - player:getHandcardNum()
      if num > 0 then
        player:drawCards(num, yijue.name)
      end
    elseif choice == "#klmou__yijue-play" then
      play = true
    end
    local tos = table.filter(room:getOtherPlayers(player), function (p) return p:isWounded() end)
    if #tos > 0 then
      local to = room:askToChoosePlayers(player, {
        targets = tos,
        max_num = 1,
        min_num = 1,
        cancelable = true,
        skill_name = yijue.name,
        prompt = "义绝：你可以令一名受伤的其他角色回复1点体力",
      })
      if #to > 0 then
        room:recover{
          who = to[1],
          num = 1,
          skillName = yijue.name,
          recoverBy = player,
        }
      end
    end
    if play then
      player:gainAnExtraPhase(Player.Play, yijue.name, false)
    end
  end,
})


return yijue
